package com.cskaoyan.gateway.controller.order;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;

import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.IMemberService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Slf4j
@Anoymous
@RestController
@RequestMapping("shopping/")
public class OrderController {

    @Reference(timeout = 3000,check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 3000, check = false)
    OrderQueryService orderQueryService;

    /**
     * 创建订单
     * @param request
     * @param request
     * @return
     */
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {



        //从request域中拿到存入的用户信息
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        //从拿到的用户信息里获取用户id并放入request里
        request.setUserId(uid);


        //设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @Anoymous
    @PutMapping("order")
    public ResponseData cancelOrder(String orderId) {
        CancelOrderResponse response = orderCoreService.cancelOrderByOrderId(orderId);
        return new ResponseUtil().setData(response);
    }


    /**
     * 删除订单接口
     * @param orderId
     * @return
     */
    @Anoymous
    @DeleteMapping("order/{orderId}")
    public ResponseData deleteOrder(@PathVariable("orderId") String orderId) {
        CancelOrderResponse response = orderCoreService.cancelOrderByOrderId(orderId);
        return new ResponseUtil().setData(response);

    }

    @GetMapping("order")
    @ApiOperation("获取当前用户所有订单")
    public ResponseData listOrders(@RequestParam("page") Integer page,
                                   @RequestParam("size") Integer size,
                                   String sort) {
        OrderListRequest request = new OrderListRequest();
        //如何获取userId？
        request.setUserId((long) 62);
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);


        OrderListResponse response = orderQueryService.listOrders(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderList orderList = new OrderList();
            orderList.setData(response.getDetailInfoList());
            orderList.setTotal(response.getTotal());
            return new ResponseUtil<>().setData(orderList);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @GetMapping("order/{id}")
    @ApiOperation("查询订单详情")
    public ResponseData order(@PathVariable("id") String id) {
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);

        OrderDetailResponse response = orderQueryService.getOrderDetailById(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderDetail detail = new OrderDetail();
            detail.setUserName(response.getBuyerNick());
            detail.setOrderTotal(response.getPayment());
            detail.setUserId(response.getUserId());
            detail.setGoodsList(response.getOrderItemDto());
            OrderShippingDto shippingDto = response.getOrderShippingDto();
            detail.setTel(shippingDto.getReceiverPhone());
            detail.setStreetName(shippingDto.getReceiverAddress());
            return new ResponseUtil<>().setData(detail);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
